/**
 * Copyright (C) 2009 Olafur Gauti Gudmundsson, Hjortur Stefan Olafsson
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.googlecode.gwt_control.client;

import com.google.gwt.user.client.DOM;
import com.google.gwt.user.client.Timer;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.Panel;
import com.google.gwt.user.client.ui.RootPanel;

/**
 *
 * @author Olafur Gauti Gudmundsson
 */
public class ApplicationView {

    private static final String STATUS_CONTAINER = "statusContainer";
    private static final String LOADING_CONTAINER = "loadingContainer";

    private final ApplicationController controller;

    private Panel statusPanel, loadingPanel;
    private HTML statusMsg;

    private PageLayout currentLayout;
    private int loadCounter;

    public ApplicationView( ApplicationController controller ) {
        this.controller = controller;

        statusPanel = new FlowPanel();
        statusPanel.setVisible(false);
        statusMsg = new HTML();
        statusPanel.setStyleName("statusPanel");
        statusPanel.add(statusMsg);
        RootPanel.get(STATUS_CONTAINER).add(statusPanel);

        loadingPanel = new FlowPanel();
        loadingPanel.setVisible(false);
        loadingPanel.setStyleName("loadingPanel");
        loadingPanel.add(new Label("Loading..."));
        loadCounter = 0;

        RootPanel.get(LOADING_CONTAINER).add(loadingPanel);
    }


    public void resetLoadCounter() {
        loadCounter = 0;
        loadingPanel.setVisible(false);
    }

    public void increaseLoadCounter() {
        loadCounter++;
        if ( loadCounter >= 1 ) {
            loadingPanel.setVisible(true);
        }
    }

    public void decreaseLoadCounter() {
        loadCounter--;
        if ( loadCounter <= 0 ) {
            loadingPanel.setVisible(false);
            loadCounter = 0;
        }
    }

    public PageLayout getCurrentPageLayout() {
        return currentLayout;
    }

    public void setStatus( HTML html ) {
        statusPanel.removeStyleName("errorMsg");
        statusMsg.setHTML(html.getHTML());
        Fader.fadeIn(statusPanel, 200);
        Timer timer = new Timer() {
            public void run() {
                Fader.fadeOut(statusPanel, 200);
            }
        };
        timer.schedule(5000);
    }

    public void setErrorMessage( HTML html ) {
        statusPanel.addStyleName("errorMsg");
        statusMsg.setHTML(html.getHTML());
        Fader.fadeIn(statusPanel, 200);
        Timer timer = new Timer() {
            public void run() {
                Fader.fadeOut(statusPanel, 200);
                statusPanel.removeStyleName("errorMsg");
            }
        };
        timer.schedule(8000);
    }

    public void setPageLayout( PageLayout pageLayout ) {
        if ( currentLayout != null ) {
            currentLayout.clear();
        }
        currentLayout = pageLayout;
        currentLayout.render();
    }
}
